
#include <stdlib.h>
#include <stdio.h>

void updateMatrix(int** p, int n)
{
for(int i = 1; i < n-1 ; i++)
  for(int j = 1; j < n-1 ; j++)
  {
     int cLive = 0;
	 // verifying neighboors in inner points
	 for(int k = i-1; k < i+2; i++)
	 {
	   if(p[k][j-1]==1) cLive++;
	   if(p[k][j+1]==1) cLive++;
	 }  
     if(p[k-1][j]==1) cLive++;
	 if(p[k+1][j]==1) cLive++;
  }
  if(p[k-1][j]==1 && cLive > 2) 
     p[k][j]== 0;  // dead by superpopulation
  if(p[k-1][j]==0 && cLive > 2) 
     p[k][j]== 1;  // born
  if(p[k-1][j]==1 && cLive < 2) 
     p[k][j]== 0;  // dead alone
	 
}


void Fill(int** p, int n)
{
	p = new int*[n];
	int i, j;
	for (i = 0; i < n; i++)
		p[i] = new int[n];


	for (i = 0; i < n; i++)
		for (j = 0; j < n; j++)
			p[i][j] = rand()%10;
}

void print(int **p, int n)
{ 
	int i, j;
	for (i=0; i<=4;i++)
	{
		for (j=0; j<=4;j++)
			printf("%d \t", p[i][j]);
		printf("\n");
	}
}

void main()
{
	const int n = 5;
	int** p;
	Fill(p,n);
	updateMatrix(p,n);
	print(p,n);
}

